package com.bean;

import org.hibernate.SessionFactory;

import com.model.UserLogin;
import com.test.LoginDAO;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.*;

/**
 * A simple Login Action.
 * 
 * @author Diego Iacono
 */
public class LoginActionBean extends BaseActionBean {
	
	private static final String VIEW = "/WEB-INF/jsp/main.jsp" ;
	@Validate(required = true)
	private String userName;
	@Validate(required = true)
	private String passName;
	private String result;
	private boolean pass = false;

	@DefaultHandler
	public Resolution loggin() {
		if (!pass) {
			result = "";
		}
		return new ForwardResolution(VIEW);
	}

	@ValidationMethod(on = "loggin")
	public void avoidDivideByZero(ValidationErrors errors) {
		if (userName != null && userName.length() > 9) {
			errors.add("userName", new SimpleError(
					"Length should be less than 9"));
		}
		LoginDAO loginDAO = new LoginDAO();
		SessionFactory factory = loginDAO.createSessionFactory();
		UserLogin user = loginDAO.queryObject( factory, userName, passName );
		if (user != null) {
			
			pass = true;
		}
		
		if (!pass) {
			errors.add("userName",new SimpleError("Login Fail"));
		}
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassName() {
		return passName;
	}

	public void setPassName(String passName) {
		this.passName = passName;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

}
